﻿/*
Copyright (C) 2010 RESSEGUIER Valentin

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Text;
using System.Threading;

using NNKirclib;

namespace NNKirclib
{
    public class IrcClient
    {
        public string Server = null; // Address of Server                Default : null                  Use : domainname or ipaddress
        public int Port = 6667; // Port of Server                        Default : 6667                  Use : port
        public string User = "guest 0 * :guest"; // User String          Default : guest 0 * :guest      Use : username + usermode + * + :realname
        public string Nick = "guest"; // Nickname                        Default : guest                 Use : nickname

        public StreamWriter writer; // Private Stream Writer
        public StreamReader reader; // Private Stream Reader
        public NetworkStream stream; // Private Stream
        public TcpClient irc; // Private Client

        public void Connect() //Command Connect()
        {
            try // Try to make : 
            {
                /* Variable Declaration */
                irc = new TcpClient(Server, Port);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);

                /* Connection Script */
                writer.WriteLine("USER " + User);
                writer.Flush();
                writer.WriteLine("NICK " + Nick);
                writer.Flush();
                Thread.Sleep(3000);

            }
            catch (Exception e)
            {
                // Show the exception, sleep for a while and try to establish a new connection to irc server
                System.Diagnostics.Debug.WriteLine(e.ToString());
                Thread.Sleep(5000);
                this.Connect();
            }
        }

        public void Join(string Channel)
        {
            if (irc.Connected)
            {
                writer.WriteLine("JOIN " + Channel);
                writer.Flush();
                Thread.Sleep(3000);
            }
        }

        public void Close()
        {
            if (irc.Connected)
            {
                try
                {
                    writer.WriteLine("QUIT");
                    writer.Flush();
                    writer.Close();
                    reader.Close();
                    Thread.Sleep(5000);
                    irc.Close();
                }
                catch (Exception e)
                {
                    // Show the exception, sleep for a while and try to establish a new connection to irc server
                    System.Diagnostics.Debug.WriteLine(e.ToString());
                    Thread.Sleep(5000);
                    this.Close();
                }
            }
        }

        public void ChannelSendMsg(string Channel, string Message)
        {
            if (irc.Connected)
            {
                writer.WriteLine("PRIVMSG " + Channel + " " + Message);
                writer.Flush();
                Thread.Sleep(5000);
            }
        }
    }
}
